<?php

namespace app\admin\controller;

use think\Db;

class Rank extends Common
{
    public function Type()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');

        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }


        $data = db('dynamic_rank_type')->where($where)->order('sort desc,id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }
    public function addcate()
    {
        $id = input("id");

        $category = Db::name('dymanic_category')->where(['is_hide' => 0, 'parent_id' => 0])->field('id,name,parent_id prev')->order('des desc,id asc')->select();

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $has = Db::name('dynamic_rank_type')->where(['title'=>$post['title']])->find();
                if ($has) return ['err'=>'名称已存在'];
                $res = Db::name('dynamic_rank_type')->insert($post);

                $data = $res ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $res = Db::name('dynamic_rank_type')->where('id', $id)->update($post);

                $data = $res ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('dynamic_rank_type')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $this->assign('category', get_tree_option1($category, 0));
            $data = $this->fetch();
        }
        return $data;
    }
    public function delcate()
    {
        if (request()->isAjax()) {
            $pid = input('id');
            if ($pid) {
                $res = Db::name('dynamic_rank_type')->delete($pid);
                if ($res) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
    public function List()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');

        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }


        $cates = db('dynamic_rank_type')->order('sort desc,id asc')->column('title','id');
        $data = db('dynamic_rank')
                    ->where($where)
                    ->order('sort desc,id asc')
                    ->paginate(_getCachePages(), false, ['query' => request()->param()])
                    ->each(function ($item) use ($cates)
                    {
                        $item['cate_name'] = $cates[$item['type_id']] ?? '';
                        return $item;
                    });

        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }
    public function add()
    {
        $id = input("id");
        $cates = db('dynamic_rank_type')->order('sort desc,id asc')->column('id,title,sort');

        if (request()->isAjax()) {
            $post = input('post.');
            $post['time'] = time();
            if (!$id) {
                $has = Db::name('dynamic_rank')->where(['type_id'=>$post['type_id'],'dy_id'=>$post['dy_id']])->find();
                if ($has) return ['err'=>'已存在'];
                $res = Db::name('dynamic_rank')->insert($post);

                $data = $res ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $res = Db::name('dynamic_rank')->where('id', $id)->update($post);

                $data = $res ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('dynamic_rank')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $this->assign('category', $cates);
            $data = $this->fetch();
        }
        return $data;
    }

    public function del()
    {
        if (request()->isAjax()) {
            $pid = input('id');
            if ($pid) {
                $res = Db::name('dynamic_rank')->delete($pid);
                if ($res) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function experience()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');

        $types = input('types', 1);
        $this->assign('types', $types);

        $where['type'] = $types;

        if ($search_name && $search_type) {
            if (in_array($search_type, ['user_id'])) {
                $where[$search_type] = $search_name;
            } else{
                $where[$search_type] = ['like', '%' . $search_name . '%'];
            }
        }

        $data = db('rank_experience')
                    ->where($where)
                    ->order('sort desc,value desc,id asc')
                    ->paginate(_getCachePages(), false, ['query' => request()->param()])
                    ->each(function ($item)
                    {
                        $item['info'] = _user_cache($item['user_id']);
                        return $item;
                    });

        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }
    public function addexperience()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');
            $post['update_time'] = time();
            if (!$id) {
                $has = Db::name('rank_experience')->where(['type'=>$post['type'],'user_id'=>$post['user_id']])->find();
                if ($has) return ['err'=>'已存在'];
                $post['create_time'] = $post['update_time'];
                $res = Db::name('rank_experience')->insert($post);

                $data = $res ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $res = Db::name('rank_experience')->where('id', $id)->update($post);

                $data = $res ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('rank_experience')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
    public function delexperience()
    {
        if (request()->isAjax()) {
            $pid = input('id');
            if ($pid) {
                $res = Db::name('rank_experience')->delete($pid);
                if ($res) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    public function experienceRefresh($type)
    {
        switch ((int)$type) {
            case 1:
                $data = Db::query('select sum(expenses) as value, server_id as user_id from privatechat_work_order group by server_id');
                break;
            case 2:
                $data = Db::query('select count(id) as value, server_id as user_id from privatechat_work_order group by server_id');
                break;
            case 3:
                $uids = Db::name('user_attestation')->where(['types'=>['in', [2,3,4]], 'status'=>1])->column('user_id');
                if (sizeof($uids)) {
                    $uids = implode(',', $uids);
                    $data = Db::query('select sum(expenses) as value, server_id as user_id from privatechat_work_order where user_id in ('.$uids.') group by server_id');
                }
                break;
            
            default:
                $data = [];
                break;
        }

        if (sizeof($data)) {
            Db::name('rank_experience')->where(['type'=>$type])->delete();
            foreach ($data as &$value) {
                $value['type'] = $type;
                $value['create_time'] = time();
            }
            Db::name('rank_experience')->insertAll($data);
        }

        return ['ret'=>'操作成功'];
    }
}